package com.ecommerce.order.dto;

import com.ecommerce.order.entity.Order;
import com.ecommerce.order.entity.OrderItem;
import com.ecommerce.order.entity.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单响应DTO
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrderResponse {

    private Long id;
    private String orderNo;
    private Long userId;
    private OrderStatus status;
    private String statusDesc;
    private Long totalAmount;
    private Long payAmount;
    private Long discountAmount;
    private Long shippingFee;
    private String receiverName;
    private String receiverPhone;
    private String receiverAddress;
    private String remark;
    private LocalDateTime payTime;
    private LocalDateTime shipTime;
    private LocalDateTime finishTime;
    private LocalDateTime cancelTime;
    private LocalDateTime createTime;
    private List<OrderItemResponse> items;

    /**
     * 从Order实体转换
     */
    public static OrderResponse fromOrder(Order order, List<OrderItem> items) {
        List<OrderItemResponse> itemResponses = items.stream()
                .map(OrderItemResponse::fromOrderItem)
                .collect(Collectors.toList());

        return OrderResponse.builder()
                .id(order.getId())
                .orderNo(order.getOrderNo())
                .userId(order.getUserId())
                .status(order.getStatus())
                .statusDesc(order.getStatus().getDescription())
                .totalAmount(order.getTotalAmount())
                .payAmount(order.getPayAmount())
                .discountAmount(order.getDiscountAmount())
                .shippingFee(order.getShippingFee())
                .receiverName(order.getReceiverName())
                .receiverPhone(order.getReceiverPhone())
                .receiverAddress(order.getReceiverAddress())
                .remark(order.getRemark())
                .payTime(order.getPayTime())
                .shipTime(order.getShipTime())
                .finishTime(order.getFinishTime())
                .cancelTime(order.getCancelTime())
                .createTime(order.getCreateTime())
                .items(itemResponses)
                .build();
    }
}